<?php
namespace app\controllers;


use app\common\corelib\LocalUpload;
use app\logic\ArticleLogic;
use yii;
//文章管理
class ArticleController extends BackendController
{
    //文章首页
    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $articleLogic = new ArticleLogic();
        $this->data['breadcrumbs'] = '文章管理';
        return $this->render('index', [
            'article' => $articleLogic->getArticle($query, $page),
            'query' => $query
        ]);
    }
    //美文欣赏
    public function actionBeautiful()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $articleLogic = new ArticleLogic();
        $this->data['breadcrumbs'] = '美文欣赏';
        return $this->render('index', [
            'article_type'=>1,
            'article' => $articleLogic->getArticle(1,$query, $page),
            'query' => $query
        ]);
    }
    //海淘图书馆
    public function actionHaiTaoLibrary()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $articleLogic = new ArticleLogic();
        $this->data['breadcrumbs'] = '海淘图书馆';
        return $this->render('index', [
            'article_type'=>2,
            'article' => $articleLogic->getArticle(2,$query, $page),
            'query' => $query
        ]);
    }
    //国际游学
    public function actionStudyTour()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $articleLogic = new ArticleLogic();
        $this->data['breadcrumbs'] = '国际游学';
        return $this->render('index', [
            'article_type'=>3,
            'article' => $articleLogic->getArticle(3,$query, $page),
            'query' => $query
        ]);
    }
    //西语日常
    public function actionDayToDay()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $articleLogic = new ArticleLogic();
        $this->data['breadcrumbs'] = '西语日常';
        return $this->render('index', [
            'article_type'=>4,
            'article' => $articleLogic->getArticle(4,$query, $page),
            'query' => $query
        ]);
    }
    //添加文章
    public function actionAdd(){
        $articleLogic = new ArticleLogic();
        if(!Yii::$app->request->post()){
            $article_type = Yii::$app->request->get('article_type', 0);
            if($article_type==1)
                $this->data['breadcrumbs'] = ['文章管理' => U('article/beautiful'), ' 添加文章' => ''];
            elseif($article_type==2)
                $this->data['breadcrumbs'] = ['文章管理' => U('article/hai-tao-library'), ' 添加文章' => ''];
            elseif($article_type==3)
                $this->data['breadcrumbs'] = ['文章管理' => U('article/study-tour'), ' 添加文章' => ''];
            elseif($article_type==4)
                $this->data['breadcrumbs'] = ['文章管理' => U('article/day-to-day'), ' 添加文章' => ''];
            return $this->render('add', [
                'article_type'=>$article_type,
                'theme' => '/assets/',
                'actionUrl' => U('article/add')
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            if($_FILES['article_pic']['name']){
                LocalUpload::go_upload($_FILES['article_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $articleLogic->addArticle($params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','新增文章成功');
                if($params['article_type']==1)
                    jump('article/beautiful');
                elseif($params['article_type']==2)
                    jump('article/hai-tao-library');
                elseif($params['article_type']==3)
                    jump('article/study-tour');
                elseif($params['article_type']==4)
                    jump('article/day-to-day');
            } else {
                Yii::$app->getSession()->setFlash('error','新增文章失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //编辑文章
    public function actionEdit(){
        $articleLogic = new ArticleLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('article_id');
            $article = $articleLogic->getById($id);
            if($article['article_type']==1)
                $this->data['breadcrumbs'] = ['文章管理' => U('article/beautiful'), $article['article_title'] => '', '更新' => ''];
            elseif($article['article_type']==2)
                $this->data['breadcrumbs'] = ['文章管理' => U('article/hai-tao-library'), $article['article_title'] => '', '更新' => ''];
            elseif($article['article_type']==3)
                $this->data['breadcrumbs'] = ['文章管理' => U('article/study-tour'), $article['article_title'] => '', '更新' => ''];
            elseif($article['article_type']==4)
                $this->data['breadcrumbs'] = ['文章管理' => U('article/day-to-day'), $article['article_title'] => '', '更新' => ''];
            if(file_exists(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$article['article_pic'])){
                $article_pic = $article['article_pic'];
            }else{
                $article_pic = '/default.jpg';
            }
            return $this->render('edit', [
                'article' => $article,
                'article_pic' => $article_pic,
                'theme' => '/assets/',
                'actionUrl' => U('article/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('article_id');
            $article = $articleLogic->getById($id);
            if($_FILES['article_pic']['name']){
                LocalUpload::go_upload($_FILES['article_pic']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $thumbs = LocalUpload::thumbs(100,100);
            }else{
                $uploadresult = '';
            }
            $logicRes = $articleLogic->editById($id, $params,$uploadresult);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新文章成功');
                if($article['article_type']==1)
                    jump('article/beautiful');
                elseif($article['article_type']==2)
                    jump('article/hai-tao-library');
                elseif($article['article_type']==3)
                    jump('article/study-tour');
                elseif($article['article_type']==4)
                    jump('article/day-to-day');
            } else {
                Yii::$app->getSession()->setFlash('error','更新文章失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //删除文章
    public function actionDelete(){
        $id = Yii::$app->request->get('article_id');
        $articleLogic = new ArticleLogic();
        $article = $articleLogic->getById($id);
        $logicRes = $articleLogic->delete($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        if($article['article_type']==1)
            return $this->redirect(U('article/beautiful'));
        elseif($article['article_type']==2)
            return $this->redirect(U('article/hai-tao-library'));
        elseif($article['article_type']==3)
            return $this->redirect(U('article/study-tour'));
        elseif($article['article_type']==4)
            return $this->redirect(U('article/day-to-day'));
    }


}